package com.jxt.myapp.service.imp;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jxt.myapp.dao.IPermissionsDao;
import com.jxt.myapp.dao.imp.PermissionsDao;
import com.jxt.myapp.domain.Group;
import com.jxt.myapp.domain.Menu;
import com.jxt.myapp.domain.User;
import com.jxt.myapp.domain.jqgrid.JqGridParameter;
import com.jxt.myapp.service.IPermissionsService;
import com.jxt.myapp.utils.WebUtils;

@Service("permissionsService")
@Transactional
public class PermissionsService implements IPermissionsService {

	@Resource(name = "permissionsDao")
	private IPermissionsDao dao;
	
	private static final Logger logger = LoggerFactory
	.getLogger(PermissionsService.class);

	public List<Group> getAllGroup(JqGridParameter jqGridParameter) {
		return dao.getAllGroup(jqGridParameter);
	}

	public long getAllGroupCount(JqGridParameter jqGridParameter) {
		return dao.getAllGroupCount(jqGridParameter);
	}

	public List<Group> getAllGroupByParameter(JqGridParameter jqGridParameter) {
		return dao.getAllGroupByParameter(jqGridParameter);
	}

	public long getAllGroupByParameterCount(JqGridParameter jqGridParameter) {
		return dao.getAllGroupByParameterCount(jqGridParameter);
	}

	public List<Menu> getAllMenu() {
		return dao.getAllMenu();
	}

	public Group getGroupById(String id) {
		return dao.getGroupById(id);
	}

	public List<String> getMenuByGroup(String groupId) {
		return dao.getMenuByGroup(groupId);
	}

	public int updateGroup(Group group, String[] menus) {
		List<Map<String, Object>> exis = dao.getGroupIdByGroupName(group
				.getName());
		if (exis.size() > 0) {
			if (!group.getId().equals(exis.get(0).get("id"))) {
				return 999;
			}
		}
		dao.deleteGroupMenu(group.getId());
		if (menus != null) {
			dao.addGroupMenu(group.getId(), menus);
		}
		return dao.updateGroup(group);
	}

	public int addGroup(Group group, String[] menus) {
		if (menus != null) {
			dao.addGroupMenu(group.getId(), menus);
		}
		return dao.addGroup(group);
	}

	public int[] deleteGroup(List<Group> groups) {
		return dao.deleteGroup(groups);
	}

	public List<User> getAllUser(JqGridParameter jqGridParameter) {
		return dao.getAllUser(jqGridParameter);
	}

	public long getAllUserCount(JqGridParameter jqGridParameter) {
		return dao.getAllUserCount(jqGridParameter);
	}

	public List<User> getAllUserByParameter(JqGridParameter jqGridParameter) {
		return dao.getAllUserByParameter(jqGridParameter);
	}

	public long getAllUserByParameterCount(JqGridParameter jqGridParameter) {
		return dao.getAllUserByParameterCount(jqGridParameter);
	}

	public int updateUser(User user) {
		return dao.updateUser(user);
	}

	public int addUser(User user) {
		return dao.addUser(user);
	}

	public User getUserById(String id) {
		return dao.getUserById(id);
	}

	public int[] deleteUser(List<User> userList) {
		return dao.deleteUser(userList);
	}

	public int deleteGroupMenu(String groupId) {
		return dao.deleteGroupMenu(groupId);
	}

	public String getAllGroupToOptions() {
		List<Map<String, Object>> list = dao.getAllGroupToOptions();
		if (list.size() == 0) {
			return null;
		}
		return WebUtils.buildSelect(list, "id", "name", null, "", false);
	}

	public int updatePassword(User user) {
		return dao.updatePassword(user);
	}

	public List<Menu> getMenuByGroupId(String groupId) {
		return dao.getMenuByGroupId(groupId);
	}

	public User getUserByLoginName(String loginName) {
		logger.info("service");
		return dao.getUserByLoginName(loginName);
	}

}
